package com.baosight.risk.web.riri;

import com.baosight.risk.api.riri.IRiriDutyService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.mapper.fltd.FltdTaskMapper;
import com.baosight.risk.mapper.jlta.JltaTaskMapper;
import com.baosight.risk.service.fltd.entity.FltdTask;
import com.baosight.risk.service.riri.entity.RiriDutyUser;
import com.baosight.risk.service.riri.vo.RiriUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * @author houji
 */
@Slf4j
@RestController
@RequestMapping("/rire/duty")
@Api(value = "评审小组维护", description = "评审小组维护", produces = MediaType.APPLICATION_JSON)
public class RiriDutyController extends BaseController {

    @Autowired
    private IRiriDutyService ririDutyService;

    @Autowired
    private FltdTaskMapper taskMapper;

    /**
     * 评审小组列表
     *
     * @param
     * @return
     */
    @GetMapping("getRiriUserList")
    @ApiOperation("结题评审小组列表")
    public AjaxResult getRiriUserList(RiriDutyUser ririDutyUser) {
        startPage();
        return getResultPage(ririDutyService.selectList(ririDutyUser));
    }


    @GetMapping("getRiriUserListValue")
    @ApiOperation("结题评审小组列表赋值")
    public AjaxResult getRiriUserListValue(@RequestParam String bizGuid) {
        List<FltdTask> fltdTasks = taskMapper.queryByGuidAndNodeCode(bizGuid,"B04");
        RiriDutyUser ririDutyUser = new RiriDutyUser();
        List<RiriDutyUser> ririDutyUsers = ririDutyService.selectList(ririDutyUser);
        List<RiriDutyUser> list = new ArrayList<>();
        for (RiriDutyUser user : ririDutyUsers){

            for(FltdTask task :fltdTasks){
                if (task.getCurrUserCode().equals(user.getUserCode())){
                    list.add(user);
                }
            }
        }

        return success(list);
    }

    @GetMapping("getRiriUserListByRifi")
    @ApiOperation("结题评审小组列表")
    public AjaxResult getRiriUserListByRifi(RiriDutyUser ririDutyUser) {
        startPage();
        return getResultPage(ririDutyService.selectListByRifi(ririDutyUser));
    }
    @PostMapping("/deleteUserByIds")
    @Log(title = "批量删除评审小组", businessType = BusinessType.DELETE)
    @ApiOperation("批量删除评审小组")
    public AjaxResult deleteEventList(@RequestBody MultipleUpdateStatus updateStatus) {
        if (CollectionUtils.isEmpty(updateStatus.getIds())) {
            log.error("请至少选择一条记录");
            return error("请至少选择一条记录");
        }
        Boolean flag = ririDutyService.deleteByIds(updateStatus);
        if (!flag) {
            log.error("有数据不符合操作规范！");
            return error("有数据不符合操作规范！");
        }
        return success();
    }

    @PostMapping("/addDutyUser")
    @Log(title = "新增成员", businessType = BusinessType.INSERT)
    @ApiOperation("新增成员")
    public AjaxResult addDutyUser(@RequestBody List<RiriDutyUser> ririDutyUserList) {
        ririDutyService.addDutyUser(ririDutyUserList);
        return success("增加成员成功");
    }

    @PostMapping("/addDutyUserByRifi")
    @Log(title = "新增成员", businessType = BusinessType.INSERT)
    @ApiOperation("新增成员")
    public AjaxResult addDutyUserByRifi(@RequestBody List<RiriDutyUser> ririDutyUserList) {
        ririDutyService.addDutyUserByRifi(ririDutyUserList);
        return success("增加成员成功");
    }

    @PostMapping("chooseDutyUser")
    @ApiOperation("挑选出当前结题审批小组成员")
    public AjaxResult chooseDutyUser(@RequestBody RiriUserVO vo) {
        ririDutyService.chooseDutyUser(vo);
        return success("已选择评审小组");
    }

    @PostMapping("doUpdateAudit")
    @ApiOperation("更新审批意见")
    public AjaxResult doUpdateAudit(@RequestBody RiriDutyUser user) {
        ririDutyService.updateAudit(user);
        return success("已选择评审意见");
    }


}



